<?php

/*
 * 
 * 这个页面专门用来配置插件功能，比如积分配置等等
 * 要使用 R/A 方法来调用这个页面的功能
 * 
 */
namespace Api\Controller\V2;
use Think\Controller;
class WidgetController extends CommonController{
    
    public function index() {
        
    }
    
    /*
     * 被调用的新增积分的操作
     * 例如
     * 注册新用户
     * 发起项目
     * 点赞
     * 评论
     * 等操作的健康豆的增减
     */
    public function member_action_bonus($action,$mid,$times) {
        /*
         * 先判断 $action 是否存在
         */
        $map['action']=$action;
        $DB_beans_setting = M("sys_beans_setting");
        $res_beans_setting = $DB_beans_setting->where($map)->find();
        
        if(!empty($res_beans_setting))
        {
            /*
             * action 正确，则写入到log 去
             */
            
            $DB_beans_log = M("sys_beans_log");
            $data['mid']=$mid;
            $data['action_name']=$res_beans_setting['action_name'];
            $data['beans']=$res_beans_setting['beans']*$times;
            $data['sid']=$res_beans_setting['id'];
            $data['action']=$res_beans_setting['action'];
            $data['ctime']=I("server.REQUEST_TIME");
            
            $DB_beans_log->add($data);
            
            
            /*
             * 更新到用户的积分和beans 去
             */
            $DB_member = M("members");
            $DB_member->where('id='.$mid)->setInc('beans',$res_beans_setting['beans']); // 用户的健康豆加减
            if($res_beans_setting['beans']>0)
            {
                $DB_member->where('id='.$mid)->setInc('credit',$res_beans_setting['beans']); // 用户的健康豆加减
            }
            
            return $data['beans'];       
            
        }
        else
        {
            return 0;       
        }
        
        
         
        
        
    }
    
    
   /*
    *  收藏功能
    *  
    */
    
   public function member_collection($ctype,$forid,$mid,$ifcollection=0) {
       /*
        * 收藏or 取消收藏
        * 0 取消收藏
        * 1 收藏
        * 
        * ctype 2 资讯，1 项目
        */
       $DB = M("collections");
       
       if($ifcollection==0)
       {
           $map['ctype']=$ctype;
           $map['for_id']=$forid;
           $map['mid']=$mid;
           
           $DB->where($map)->delete();
       }
       else
       {
           //新增
           $data['ctype']=$ctype;
           $data['for_id']=$forid;
           $data['mid']=$mid;
           $data['ctime']=I("server.REQUEST_TIME");
           
           
           $DB->add($data,array(),true);
           
       }
   }
   
   /*
    * 评论功能
    * 
    * 添加
    * 
    * ctype 评论类型 1项目 2资讯 3广告专题
    * 
    */
   public function comment_add($pid=0,$mid=0,$content="",$ctype=0,$for_id=0) {
       $data['pid']=$pid;
       $data['mid']=$mid;
       $data['content']=$content;
       $data['ctype']=$ctype;
       $data['for_id']=$for_id;
       $data['ctime']=I("server.REQUEST_TIME");
       $data['ishidden']=0;
       
       $DB = M("comments");
       $id = $DB->add($data);
       if($id)
       {
           $this->comment_like($id, $mid);
           
           return $id;
       }
       else
       {
           return FALSE;
       }
       
   }
   
   
   /*
    * 评论添加点赞记录
    */
   public function comment_like($cid,$mid) {
       $data['cid']=$cid;
       $data['mid']=$mid;
       $data['ctime']=I("server.REQUEST_TIME");
       
       $DB = M("comment_likes");
       $DB->add($data);
       
   }
}